﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Text;

namespace SkyReader
{
    public class PageBuilder
    {
        private TextBlock measureBlock;
        private StringBuilder sbContent;
        private int width;

        public PageBuilder(TextBlock textBlock)
        {
            this.measureBlock = new TextBlock();
            this.measureBlock.TextWrapping = textBlock.TextWrapping;
            this.measureBlock.FontSize = textBlock.FontSize;
            this.measureBlock.FontFamily = textBlock.FontFamily;
            this.measureBlock.FontWeight = textBlock.FontWeight;
            this.measureBlock.Foreground = textBlock.Foreground;
            this.measureBlock.Margin = new Thickness(0, 0, this.MeasureString(" ").Width, 0);
            this.measureBlock.Width = textBlock.Width;
            this.measureBlock.Height = textBlock.Height;

            this.width = (int)textBlock.Width;
            this.sbContent = new StringBuilder();
        }


        public bool Append(char c)
        {
            this.sbContent.Append(c);

            this.measureBlock.Text = this.sbContent.ToString();
            if (this.measureBlock.ActualHeight > 700)
            {
                this.sbContent.Remove(this.sbContent.Length - 1, 1);
                return true;
            }
            return false;
        }

        public string Append(string line)
        {
            StringBuilder sbTemp = new StringBuilder();   
            sbTemp.Append(sbContent.ToString());
            sbTemp.Append(line);


            this.measureBlock.Text = sbTemp.ToString();

            if (this.measureBlock.ActualHeight > 700)
            {
                sbTemp = new StringBuilder();
                sbTemp.Append(sbContent.ToString());
                for (int index = 0; index < line.Length; index++)
                {
                    sbTemp.Append(line[index]);
                    this.measureBlock.Text = sbTemp.ToString();
                    if (this.measureBlock.ActualHeight > 700)
                    {
                        if(index<1)
                        {
                            return line;
                        }
                        this.sbContent.Append(line.Substring(0,index));
                        return line.Substring(index);
                    }
                }
                return string.Empty;
            }
            sbContent.Append(line);
            return string.Empty;             
        }

        public string Content
        {
            get
            {
                string content = this.sbContent.ToString();
                return content;
            }
        }

        private Size MeasureString(string text)
        {
            this.measureBlock.Text = text;
            return new Size(this.measureBlock.ActualWidth, this.measureBlock.ActualHeight);
        }
    }

    public class PageHistory
    {
        public string OverflowContent { get; set; }
        public int CurrentIndex{get;set;}
    }
}
